<?php
/**
 * 当事人控制器
 */

declare (strict_types = 1);

namespace app\controller\lawcase;

use app\BaseController;
use app\model\lawcase\Party as PartyModel;
use app\model\lawcase\LawcaseParty as LawcasePartyModel;
use think\facade\Db;
use think\facade\Request;

class Party extends BaseController
{
    /**
     * 获取当事人列表
     */
    public function list()
    {
        try {
            $param = Request::param();
            $page = isset($param['page']) ? (int)$param['page'] : 1;
            $limit = isset($param['limit']) ? (int)$param['limit'] : 10;
            
            // 构建查询条件
            $where = [];
            $where[] = ['p.delete_time', '=', 0];
            
            // 处理搜索关键词
            $keywords = isset($param['keywords']) ? trim($param['keywords']) : '';
            if (!empty($keywords)) {
                $where[] = ['p.name|p.id_number', 'like', '%' . $keywords . '%'];
            }
            
            // 按案件ID筛选（仅在明确指定case_id时才应用此筛选）
            if (!empty($param['case_id'])) {
                $where[] = ['lp.case_id', '=', $param['case_id']];
                $where[] = ['lp.delete_time', '=', 0]; // 只对特定案件筛选已删除的关联记录
                
                // 查询案件关联的当事人
                $query = Db::name('lawcase_party')
                    ->alias('lp')
                    ->join('party p', 'lp.party_id = p.id', 'LEFT')
                    ->field([
                        'lp.*',
                        'p.name as partyName',
                        'p.party_type as partyType',
                        'p.party_source as partySource',
                        'p.contract_id as contractId',
                        'p.contact_id as contactId',
                        'p.id_number as idNumber',
                        'p.contact as contactPerson',
                        'p.phone as contactPhone',
                        'p.address as address',
                        'p.create_time' // 添加创建时间字段
                    ])
                    ->where($where);
                
                // 查询总数
                $count = $query->count();
                
                // 分页查询
                $list = $query
                    ->page($page, $limit)
                    ->order('lp.id', 'desc')
                    ->select()
                    ->each(function($item) {
                        // 当事人类型名称
                        $partyTypeNames = [
                            1 => '个人',
                            2 => '企业'
                        ];
                        $item['party_type_name'] = isset($partyTypeNames[$item['partyType']]) ? $partyTypeNames[$item['partyType']] : '个人';
                        
                        // 当事人来源名称
                        $partySourceNames = [
                            1 => '客户本身',
                            2 => '客户联系人',
                            3 => '外部人员'
                        ];
                        $item['party_source_name'] = isset($partySourceNames[$item['partySource']]) ? $partySourceNames[$item['partySource']] : '未知';
                        
                        // 委托身份文本映射
                        $roleTypes = [
                            1 => '委托人',
                            2 => '对方当事人',
                            3 => '第三人'
                        ];
                        $item['is_client_text'] = isset($roleTypes[$item['is_client']]) ? $roleTypes[$item['is_client']] : '对方当事人';
                        
                        // 诉讼地位名称
                        $item['relationship_text'] = LawcasePartyModel::getRelationshipText($item['relationship']);
                        
                        return $item;
                    });
            } else {
                // 查询所有有效当事人（不关联特定案件）
                $query = Db::name('party')
                    ->alias('p')
                    ->field('p.id, p.name as partyName, p.party_type as partyType, p.party_source as partySource, p.contract_id as contractId, p.contact_id as contactId, p.id_number as idNumber, p.contact as contactPerson, p.phone as contactPhone, p.address, p.create_time')
                    ->where($where);
                
                // 查询总数
                $count = $query->count();
                
                // 分页查询
                $list = $query
                    ->page($page, $limit)
                    ->order('p.id', 'desc')
                    ->select()
                    ->each(function($item) {
                        // 当事人类型名称
                        $partyTypeNames = [
                            1 => '个人',
                            2 => '企业'
                        ];
                        $item['party_type_name'] = isset($partyTypeNames[$item['partyType']]) ? $partyTypeNames[$item['partyType']] : '个人';
                        
                        // 当事人来源名称
                        $partySourceNames = [
                            1 => '客户本身',
                            2 => '客户联系人',
                            3 => '外部人员'
                        ];
                        $item['party_source_name'] = isset($partySourceNames[$item['partySource']]) ? $partySourceNames[$item['partySource']] : '未知';
                        
                        // 设置默认值
                        $item['is_client_text'] = '未关联';
                        $item['relationship_text'] = '未关联';
                        
                        return $item;
                    });
            }
            
            return $this->paginate($list->toArray(), $count, $page, $limit, 'success');
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '获取失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 查看当事人详情
     */
    public function view()
    {
        $param = Request::param();
        
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        try {
            // 查询当事人详情
            $party = Db::name('lawcase_party')
                ->alias('lp')
                ->join('party p', 'lp.party_id = p.id', 'LEFT')
                ->field([
                    'lp.*',
                    'p.name as partyName',
                    'p.party_type as partyType',
                    'p.party_source as partySource',
                    'p.contract_id as contractId',
                    'p.contact_id as contactId',
                    'p.id_number as idNumber',
                    'p.legal_representative as legalRepresentative',
                    'p.contact as contactPerson',
                    'p.phone as contactPhone',
                    'p.pid',
                    'p.cid',
                    'p.did',
                    'p.address'
                ])
                ->where(['lp.id' => $param['id'], 'lp.delete_time' => 0])
                ->find();
            
            if (empty($party)) {
                return json(['code' => 404, 'msg' => '当事人不存在']);
            }
            
            // 当事人类型名称
            $partyTypeNames = [
                1 => '个人',
                2 => '企业'
            ];
            $party['party_type_name'] = isset($partyTypeNames[$party['partyType']]) ? $partyTypeNames[$party['partyType']] : '未知';
            
            // 当事人来源名称
            $partySourceNames = [
                1 => '客户本身',
                2 => '客户联系人',
                3 => '外部人员'
            ];
            $party['party_source_name'] = isset($partySourceNames[$party['partySource']]) ? $partySourceNames[$party['partySource']] : '未知';
            
            // 委托身份文本映射
            $roleTypes = [
                1 => '委托人',
                2 => '对方当事人',
                3 => '第三人'
            ];
            $party['is_client_text'] = isset($roleTypes[$party['is_client']]) ? $roleTypes[$party['is_client']] : '未知角色';
            
            // 诉讼地位名称
            $party['relationship_text'] = LawcasePartyModel::getRelationshipText($party['relationship']);
            
            // 获取省市区信息
            if ($party['pid'] > 0) {
                $party['province_name'] = Db::name('region')->where('id', $party['pid'])->value('title');
            }
            
            if ($party['cid'] > 0) {
                $party['city_name'] = Db::name('region')->where('id', $party['cid'])->value('title');
            }
            
            if ($party['did'] > 0) {
                $party['district_name'] = Db::name('region')->where('id', $party['did'])->value('title');
            }
            
            return json([
                'code' => 0,
                'msg' => 'success',
                'data' => $party
            ]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '获取失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 新增当事人
     */
    public function add()
    {
        $param = Request::param();
        
        // 数据验证
        if (empty($param['case_id'])) {
            return json(['code' => 400, 'msg' => '案件ID不能为空']);
        }
        
        // 开始事务
        Db::startTrans();
        
        try {
            // 如果提供了party_id，则使用现有当事人
            if (!empty($param['party_id'])) {
                // 检查当事人是否存在
                $party = Db::name('party')
                    ->where('id', $param['party_id'])
                    ->where('delete_time', 0)
                    ->find();
                
                if (!$party) {
                    return json(['code' => 404, 'msg' => '当事人不存在']);
                }
                
                // 检查案件当事人是否已存在
                $exists = Db::name('lawcase_party')
                    ->where('case_id', $param['case_id'])
                    ->where('party_id', $param['party_id'])
                    ->where('delete_time', 0)
                    ->find();
                
                if ($exists) {
                    return json(['code' => 400, 'msg' => '该当事人已添加到案件中']);
                }
                
                $party_id = $param['party_id'];
            }
            // 如果没有提供party_id，但提供了party_name，则创建新当事人
            else if (!empty($param['party_name'])) {
                // 准备party数据，支持统一字段名称同时保持向后兼容
                $partyData = [
                    'name' => $param['partyName'] ?? $param['party_name'],
                    'party_type' => $param['partyType'] ?? $param['party_type'] ?? 1,
                    'party_source' => $param['partySource'] ?? $param['party_source'] ?? 3, // 默认为外部人员
                    'contract_id' => $param['contractId'] ?? $param['contract_id'] ?? 0,
                    'contact_id' => $param['contactId'] ?? $param['contact_id'] ?? 0,
                    'id_number' => $param['idNumber'] ?? $param['id_number'] ?? '',
                    'contact' => $param['contactPerson'] ?? $param['contact'] ?? '',
                    'phone' => $param['contactPhone'] ?? $param['phone'] ?? $param['mobile'] ?? '', // 支持统一字段和现有字段
                    'create_time' => time(),
                    'update_time' => time(),
                    'delete_time' => 0
                ];
                
                // 插入当事人记录
                $party_id = Db::name('party')->insertGetId($partyData);
                
                if (!$party_id) {
                    throw new \Exception('创建当事人失败');
                }
            }
            else {
                return json(['code' => 400, 'msg' => '当事人信息不完整']);
            }
            
            // 插入案件当事人关联记录
            $lawcasePartyData = [
                'case_id' => $param['case_id'],
                'party_id' => $party_id,
                'is_client' => $param['is_client'] ?? 0, // 是否委托人 0-非委托人，1-委托人
                'relationship' => $param['relationship'] ?? 0, // 法律关系
                'create_time' => time(),
                'update_time' => time(),
                'delete_time' => 0
            ];
            
            $lawcasePartyId = Db::name('lawcase_party')->insertGetId($lawcasePartyData);
            
            // 提交事务
            Db::commit();
            
            return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $lawcasePartyId]]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '保存失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 编辑当事人
     */
    public function edit()
    {
        $param = Request::param();
        
        // 数据验证
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        // 检查案件当事人是否存在
        $lawcaseParty = Db::name('lawcase_party')
            ->where('id', $param['id'])
            ->where('delete_time', 0)
            ->find();
        
        if (!$lawcaseParty) {
            return json(['code' => 404, 'msg' => '案件当事人不存在']);
        }
        
        // 准备lawcase_party表的更新数据
        $lpData = [
            'id' => $param['id'],
            'is_client' => $param['is_client'] ?? $lawcaseParty['is_client'],
            'relationship' => $param['relationship'] ?? $lawcaseParty['relationship'],
            'update_time' => time()
        ];
        
        // 检查party_id是否存在
        if (empty($lawcaseParty['party_id'])) {
            return json(['code' => 404, 'msg' => '当事人信息不完整']);
        }
        
        // 准备party表的更新数据
        $partyData = [];
        
        // 如果有设置这些字段，则更新party表
        if (isset($param['party_name'])) {
            $partyData['name'] = $param['party_name'];
        }
        
        if (isset($param['party_type'])) {
            $partyData['party_type'] = $param['party_type'];
        }
        
        if (isset($param['party_source'])) {
            $partyData['party_source'] = $param['party_source'];
        }
        
        if (isset($param['contract_id'])) {
            $partyData['contract_id'] = $param['contract_id'];
        }
        
        if (isset($param['contact_id'])) {
            $partyData['contact_id'] = $param['contact_id'];
        }
        
        if (isset($param['id_number'])) {
            $partyData['id_number'] = $param['id_number'];
        }
        
        if (isset($param['contact'])) {
            $partyData['contact'] = $param['contact'];
        }
        
        if (isset($param['mobile'])) {
            $partyData['phone'] = $param['mobile'];
        }
        
        // 如果有party表数据需要更新，则添加更新时间
        if (!empty($partyData)) {
            $partyData['update_time'] = time();
        }
        
        try {
            // 开启事务
            Db::startTrans();
            
            // 更新案件当事人关联表
            Db::name('lawcase_party')->update($lpData);
            
            // 如果有party表数据需要更新，则更新party表
            if (!empty($partyData)) {
                $partyData['id'] = $lawcaseParty['party_id'];
                Db::name('party')->update($partyData);
            }
            
            // 提交事务
            Db::commit();
            
            return json(['code' => 0, 'msg' => '更新成功', 'data' => ['id' => $param['id']]]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 500, 'msg' => '更新失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 删除当事人（软删除）
     */
    public function delete()
    {
        $param = Request::param();
        
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        try {
            // 检查案件当事人是否存在
            $lawcaseParty = Db::name('lawcase_party')
                ->where('id', $param['id'])
                ->where('delete_time', 0)
                ->find();
            
            if (!$lawcaseParty) {
                return json(['code' => 404, 'msg' => '案件当事人不存在']);
            }
            
            // 软删除案件当事人
            Db::name('lawcase_party')
                ->where('id', $param['id'])
                ->update([
                    'delete_time' => time(),
                    'update_time' => time()
                ]);
            
            return json(['code' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '删除失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 选择当事人列表（用于下拉选择）
     */
    public function select_list()
    {
        try {
            $param = Request::param();
            
            // 构建查询条件
            $where = [];
            $where[] = ['delete_time', '=', 0];
            
            // 关键词搜索
            if (!empty($param['keywords'])) {
                $where[] = ['name|id_number', 'like', '%' . trim($param['keywords']) . '%'];
            }
            
            // 查询当事人列表
            $list = Db::name('party')
                ->where($where)
                ->order('id', 'desc')
                ->select()
                ->each(function($item) {
                    // 添加当事人类型和来源文本
                    $item['party_type_text'] = PartyModel::getPartyTypeText($item['party_type']);
                    $item['party_source_text'] = PartyModel::getPartySourceText($item['party_source']);
                    return $item;
                });
            
            return json([
                'code' => 0,
                'msg' => 'success',
                'data' => $list
            ]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '获取失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 解除当事人关联（软删除案件当事人关联记录）
     */
    public function unlink()
    {
        $param = Request::param();
        
        if (empty($param['id'])) {
            return json(['code' => 400, 'msg' => '参数错误']);
        }
        
        try {
            // 检查案件当事人是否存在
            $lawcaseParty = Db::name('lawcase_party')
                ->where('id', $param['id'])
                ->where('delete_time', 0)
                ->find();
            
            if (!$lawcaseParty) {
                return json(['code' => 404, 'msg' => '案件当事人关联记录不存在']);
            }
            
            // 软删除案件当事人关联记录
            Db::name('lawcase_party')
                ->where('id', $param['id'])
                ->update([
                    'delete_time' => time(),
                    'update_time' => time()
                ]);
            
            return json(['code' => 0, 'msg' => '解除关联成功']);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '解除关联失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 根据合同ID获取相关当事人
     */
    public function get_parties_by_contract()
    {
        try {
            $param = Request::param();
            
            // 支持直接参数和嵌套在params中的参数
            $contract_id = $param['contract_id'] ?? ($param['params']['contract_id'] ?? '');
            
            if (empty($contract_id)) {
                return json(['code' => 400, 'msg' => '合同ID不能为空']);
            }
            
            // 获取合同相关的客户信息
            $contract = Db::name('contract')
                ->where('id', $contract_id)
                ->where('delete_time', 0)
                ->find();
            
            if (!$contract) {
                return json(['code' => 404, 'msg' => '合同不存在']);
            }
            
            $parties = [];
            
            // 添加客户本身作为当事人选项
            if ($contract['customer_id'] > 0) {
                $customer = Db::name('customer')
                    ->where('id', $contract['customer_id'])
                    ->where('delete_time', 0)
                    ->find();
                
                if ($customer) {
                    $parties[] = [
                        'id' => 0, // 临时ID，表示新当事人
                        'name' => $customer['name'],
                        'party_type' => 1, // 个人
                        'party_source' => 1, // 客户本身
                        'contract_id' => $contract['id'],
                        'contact_id' => 0,
                        'party_type_text' => '个人',
                        'party_source_text' => '客户本身'
                    ];
                }
            }
            
            // 添加客户联系人作为当事人选项
            if ($contract['customer_id'] > 0) {
                $contacts = Db::name('customer_contact')
                    ->where('cid', $contract['customer_id'])
                    ->where('delete_time', 0)
                    ->select();
                
                foreach ($contacts as $contact) {
                    $parties[] = [
                        'id' => 0, // 临时ID，表示新当事人
                        'name' => $contact['name'],
                        'party_type' => 1, // 个人
                        'party_source' => 2, // 客户联系人
                        'contract_id' => $contract['id'],
                        'contact_id' => $contact['id'],
                        'party_type_text' => '个人',
                        'party_source_text' => '客户联系人'
                    ];
                }
            }
            
            // 添加外部当事人作为选项
            $externalParties = Db::name('party')
                ->where('contract_id', $contract_id)
                ->where('party_source', 3) // 外部人员
                ->where('delete_time', 0)
                ->select();
                
            foreach ($externalParties as $externalParty) {
                $parties[] = [
                    'id' => $externalParty['id'],
                    'name' => $externalParty['name'],
                    'party_type' => $externalParty['party_type'],
                    'party_source' => 3, // 外部人员
                    'contract_id' => $externalParty['contract_id'],
                    'contact_id' => 0,
                    'id_number' => $externalParty['id_number'],
                    'phone' => $externalParty['phone'],
                    'contact' => $externalParty['contact'],
                    'address' => $externalParty['address'],
                    'party_type_text' => $externalParty['party_type'] == 1 ? '个人' : '企业',
                    'party_source_text' => '外部当事人'
                ];
            }
            
            return json(['code' => 0, 'msg' => 'success', 'data' => $parties]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '获取当事人失败: ' . $e->getMessage()]);
        }
    }
    
    /**
     * 根据当事人ID获取相关案件列表
     */
    public function get_related_cases()
    {
        try {
            $param = Request::param();
            $party_id = $param['party_id'] ?? '';
            
            if (empty($party_id)) {
                return json(['code' => 400, 'msg' => '当事人ID不能为空']);
            }
            
            // 分页参数
            $page = intval($param['page'] ?? 1);
            $limit = intval($param['limit'] ?? 10);
            
            // 检查当事人是否存在
            $party = Db::name('party')
                ->where('id', $party_id)
                ->where('delete_time', 0)
                ->find();
                
            if (!$party) {
                return json(['code' => 404, 'msg' => '当事人不存在']);
            }
            
            // 通过lawcase_party表关联查询案件
            $query = Db::name('lawcase')
                ->alias('l')
                ->join('lawcase_party lp', 'l.id = lp.case_id')
                ->where('lp.party_id', $party_id)
                ->where('l.delete_time', 0)
                ->where('lp.delete_time', 0);
                
            // 获取总数
            $total = $query->count();
            
            // 查询案件列表
            $list = $query->field('l.*')
                ->order('l.id', 'desc')
                ->page($page, $limit)
                ->select()
                ->each(function($item) use ($party_id) {
                    // 添加状态文本
                    $statusTexts = [
                        0 => '待受理',
                        1 => '进行中',
                        2 => '已结案',
                        3 => '已归档'
                    ];
                    $status = isset($item['status']) ? (int)$item['status'] : 0;
                    $item['status_text'] = $statusTexts[$status] ?? '未知';
                    
                    // 添加案件类型文本
                    $typeTexts = [
                        1 => '民事诉讼',
                        2 => '刑事诉讼',
                        3 => '行政诉讼',
                        4 => '仲裁',
                        5 => '执行'
                    ];
                    $caseType = $item['case_type'] ?? $item['type'] ?? '';
                    if (is_numeric($caseType)) {
                        $caseType = (int)$caseType;
                        $item['case_type_text'] = $typeTexts[$caseType] ?? '未知';
                    } else {
                        // 如果是中文，直接使用
                        $item['case_type_text'] = $caseType ?: '未知';
                    }
                    
                    // 获取案件当事人信息
                    $parties = Db::name('lawcase_party')
                        ->alias('lp')
                        ->join('party p', 'lp.party_id = p.id')
                        ->where('lp.case_id', $item['id'])
                        ->where('lp.delete_time', 0)
                        ->where('p.delete_time', 0)
                        ->select();
                    
                    $clients = [];
                    $opponents = [];
                    
                    foreach ($parties as $party) {
                        if ($party['is_client'] == 1) {
                            $clients[] = $party['name'];
                        } else {
                            $opponents[] = $party['name'];
                        }
                    }
                    
                    $item['clients'] = implode(', ', $clients);
                    $item['opponents'] = implode(', ', $opponents);
                    
                    // 获取当前当事人在此案件中的诉讼地位
                    $currentParty = Db::name('lawcase_party')
                        ->alias('lp')
                        ->join('party p', 'lp.party_id = p.id')
                        ->where('lp.case_id', $item['id'])
                        ->where('lp.party_id', $party_id)
                        ->where('lp.delete_time', 0)
                        ->where('p.delete_time', 0)
                        ->find();
                    
                    if ($currentParty) {
                        // 添加诉讼地位文本
                        $relationshipTexts = [
                            1 => '原告',
                            2 => '被告',
                            3 => '第三人',
                            4 => '申请人',
                            5 => '被申请人',
                            6 => '上诉人',
                            7 => '被上诉人',
                            8 => '再审申请人',
                            9 => '被申请人（再审）',
                            10 => '执行申请人',
                            11 => '被执行人'
                        ];
                        $relationship = $currentParty['relationship'] ?? 0;
                        $item['relationship'] = $relationship;
                        $item['relationship_text'] = $relationshipTexts[$relationship] ?? '未知';
                        
                        // 添加委托身份文本
                        $isClientTexts = [
                            0 => '非委托人',
                            1 => '委托人',
                            2 => '委托人'
                        ];
                        $isClient = $currentParty['is_client'] ?? 0;
                        $item['is_client'] = $isClient;
                        $item['is_client_text'] = $isClientTexts[$isClient] ?? '未知';
                    } else {
                        $item['relationship'] = 0;
                        $item['relationship_text'] = '未知';
                        $item['is_client'] = 0;
                        $item['is_client_text'] = '未知';
                    }
                    
                    // 获取法院名称
                    if ($item['court_id'] > 0) {
                        $item['court_name'] = Db::name('court')
                            ->where('id', $item['court_id'])
                            ->where('delete_time', 0)
                            ->value('name');
                    }
                    
                    // 获取案由名称
                    if ($item['case_cause_id'] > 0) {
                        $item['case_cause_name'] = Db::name('case_cause')
                            ->where('id', $item['case_cause_id'])
                            ->where('delete_time', 0)
                            ->value('name');
                    }
                    
                    // 获取律师信息
                    if ($item['main_lawyer'] > 0) {
                        $item['main_lawyer_name'] = Db::name('admin')
                            ->where('id', $item['main_lawyer'])
                            ->value('name');
                    }
                    
                    if ($item['secondary_lawyer'] > 0) {
                        $item['secondary_lawyer_name'] = Db::name('admin')
                            ->where('id', $item['secondary_lawyer'])
                            ->value('name');
                    }
                    
                    if ($item['session_lawyer'] > 0) {
                        $item['session_lawyer_name'] = Db::name('admin')
                            ->where('id', $item['session_lawyer'])
                            ->value('name');
                    }
                    
                    return $item;
                });
            
            return json([
                'code' => 0,
                'msg' => 'success',
                'data' => [
                    'list' => $list,
                    'total' => $total,
                    'page' => $page,
                    'limit' => $limit
                ]
            ]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => '获取相关案件失败: ' . $e->getMessage()]);
        }
    }
}